package demo;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
 
public class SipClient {
    private String host;
    private int port;
 
    public SipClient(String host, int port) {
        this.host = host;
        this.port = port;
    }
 
    public void run() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new ChannelInitializer<NioSocketChannel>() {
                 @Override
                 public void initChannel(NioSocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new SimpleChannelInboundHandler<Object>() {
                         @Override
                         protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
                             System.out.println(msg);
                         }
                     });
                     // 添加处理SIP消息的handler
                 }
             });

            ChannelFuture f = b.connect(host, port).sync();
            f.channel().writeAndFlush("测试");
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
 
    public static void main(String[] args) throws Exception {
        String host = "127.0.0.1"; // SIP服务器地址
        int port = 5062; // SIP服务器端口
        new SipClient(host, port).run();
    }
}